
package com.imooc.sell.controller;

import com.google.gson.JsonObject;
import com.imooc.sell.dataobject.ProductCategory;
import com.imooc.sell.dataobject.query.CategoryQuery;
import com.imooc.sell.service.ProductInfoService;
import com.imooc.sell.service.impl.CategoryServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @ClassName CategoryController
 * @Description 后台类目管理
 * @Author tianli
 * @Date 2018/8/14
 **/
@Controller
@RequestMapping("/admin")
public class CategoryController {

    @Autowired
    CategoryServiceImpl categoryService;

    @Autowired
    ProductInfoService productInfoService;


    private Date createTimeForAdmin = null;

    private Date endTimeForAdmin = null;

    private Integer sortCodeForAdmin;

    /**
     * @return com.imooc.sell.VO.tian.newUtil.ResultVo
     * @Author tianli
     * @Description 查询商品所有种类
     * @Date 2018/8/14
     **/
    @RequestMapping("/categorylist")
    public ModelAndView categoryList(String openid, @RequestParam(value = "page", defaultValue = "1") int start, @RequestParam(value = "size", defaultValue = "2") int size) {
        ModelAndView modelAndView = new ModelAndView();

        //进行分页
        Sort sort = new Sort(Sort.Direction.ASC, "id"); //默认设置根据id升序排列
        Pageable pageable = new PageRequest(start - 1, size, sort); //根据start、size、sort创建分页对象
        Page<ProductCategory> page = categoryService.findAll(pageable);    //根据这个分页对象获取分页对象

        //返回视图
        modelAndView.setViewName("categorylist");
        modelAndView.addObject("page", page);
        modelAndView.addObject("currentPage", start - 1);
        modelAndView.addObject("size", size);
        modelAndView.addObject("sortCode", 2);
        modelAndView.addObject("openid", openid);
        return modelAndView;
    }

    /**
     * @return com.imooc.sell.VO.tian.newUtil.ResultVo
     * @Author tianli
     * @Description 删除商品所有种类
     * @Date 2018/8/14
     **/
    @RequestMapping("/categorydelete")
    @ResponseBody
    public String categoryDelete(String openid, Integer id) {
        JsonObject jsonObject = new JsonObject();
        //首先删除相关商品
        ProductCategory productCategory = categoryService.getOne(id);
        Boolean flag = productInfoService.deleteByCategoryType(productCategory.getCategoryType());
        if (categoryService.deletCategory(id) && flag) {
            jsonObject.addProperty("msg", 1);
        } else {
            jsonObject.addProperty("msg", 0);
        }
        jsonObject.addProperty("openid", openid);
        return jsonObject.toString();
    }

    /**
     * @return com.imooc.sell.VO.tian.newUtil.ResultVo
     * @Author tianli
     * @Description 增加商品种类
     * @Date 2018/8/14
     **/
    @RequestMapping(value = "/categorysave", method = RequestMethod.POST)
    @ResponseBody
    public String categoryAdd(String openid, ProductCategory productCategory, HttpServletRequest request) {

        JsonObject jsonObject = new JsonObject();
        if (productCategory.getId() == null) {
            if (categoryService.save(productCategory) != null) {
                jsonObject.addProperty("msg", 1);//添加成功
            } else {
                jsonObject.addProperty("msg", 0);//添加失败
            }
        } else {
            if (categoryService.save(productCategory) != null) {
                jsonObject.addProperty("msg", 3);//修改成功
            } else {
                jsonObject.addProperty("msg", 2);//修改失败
            }
        }
        jsonObject.addProperty("openid", openid);
        return jsonObject.toString();
    }

    @RequestMapping("categorylistbyid")
    public ModelAndView categoryListById(String openid, Integer sortCode, CategoryQuery categoryQuery, @RequestParam(value = "page", defaultValue = "1") int start, @RequestParam(value = "size", defaultValue = "2") int size) {
        if (sortCode != null) sortCodeForAdmin = sortCode;
        ModelAndView modelAndView = new ModelAndView();
        //进行分页
        Page<ProductCategory> page = categoryService.findCategoryCrCriteria(start - 1, size, categoryQuery, sortCodeForAdmin);
        modelAndView.setViewName("categorylist");

        modelAndView.addObject("sortCode", sortCodeForAdmin);
        modelAndView.addObject("page", page);
        modelAndView.addObject("currentPage", start - 1);
        modelAndView.addObject("size", size);
        modelAndView.addObject("openid", openid);
        return modelAndView;
    }

    @RequestMapping("/categorylistbytime")
    public ModelAndView categoryListByTime(String openid, @DateTimeFormat(pattern = "yyyy-MM-dd") Date createTime, @DateTimeFormat(pattern = "yyyy-MM-dd") Date endTime, @RequestParam(value = "page", defaultValue = "1") int start, @RequestParam(value = "size", defaultValue = "2") int size) {
        if (createTime != null) {
            createTimeForAdmin = createTime;
        }
        if (endTime != null) {
            endTimeForAdmin = endTime;
        }
        ModelAndView modelAndView = new ModelAndView();
        Page<ProductCategory> page = categoryService.findCategoryByTime(createTimeForAdmin, endTimeForAdmin, start - 1, size);
        //返回视图
        modelAndView.setViewName("categorylist");
        modelAndView.addObject("page", page);
        modelAndView.addObject("currentPage", start - 1);
        modelAndView.addObject("size", size);
        modelAndView.addObject("sortCode", 2);
        modelAndView.addObject("openid", openid);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");//要转换的时间格式
        if (createTimeForAdmin != null) {
            modelAndView.addObject("createTime", sdf.format(createTimeForAdmin));
        } else {
            modelAndView.addObject("createTime", "0000-00-00");
        }
        if (endTimeForAdmin != null) {
            modelAndView.addObject("endTime", sdf.format(endTimeForAdmin));
        } else {
            modelAndView.addObject("endTime", "0000-00-00");
        }
        return modelAndView;
    }
}